<?php
/**
 * Memcache连接处理类
 *
 * @author ananda
 */
class Connection_Memcache extends Connection_Base
{

    /**
     * 连接驱动创建及初始化
     */
    public function  init()
    {
        //创建对象
        $this->driver = new Memcache;

        //判断是连接单个服务器或是同时连接多台服务器
        if (is_array($this->conf['host'])) {

            $persistent = isset($this->conf['persistent']) ? $this->conf['persistent'] : true;
            $timeout    = isset($this->conf['timeout'])    ? $this->conf['timeout']    : 1;

            foreach ($this->conf['host'] as $key => $value) {
                $this->driver->addServer($value, $this->conf['port'][$key], $persistent,
                                         $this->conf['weight'][$key], $timeout);
            }

        } else {

            $timeout = isset($this->conf['timeout']) ? $this->conf['timeout'] : 1;
            if (isset($this->conf['persistent']) && $this->conf['persistent']) {
                $this->driver->pconnect($this->conf['host'], $this->conf['port'], $timeout);
            } else {
                $this->driver->connect($this->conf['host'], $this->conf['port'], $timeout);
            }

        }
    }

    public function read($key, $flag = false)
    {
        if (!is_array($key)) {
            $key = array($key);
        }

        $result = $this->driver->get($key, $flag);
        if ($result === false) {
            return new Error('memcache key not exists');
        }
        return $this->buildResult('Data_Collection_Memcache', $result);
    }

    public function readOne($key, $flag = false)
    {
        $result = $this->driver->get($key, $flag);
        if ($result === false) {
            return new Error("memcache key [$key] not exists");
        }
        return is_array($result) ? $this->buildResult('Data_Record_Memcache', $result) : $result;
    }

    public function create($key, $value, $flag = false, $expire = 0)
    {
        if (!$this->driver->add($key, $value, $flag, $expire)) {
            return new Error('Add key fail, maybe memcache key exists');
        }
        return true;
    }

    public function update($key, $value, $flag = false, $expire = 0)
    {
        if (!$this->driver->set($key, $value, $flag, $expire)) {
            return new Error('Set key fail');
        }
        return true;
    }

    public function delete($key, $timeout = 0)
    {
        if (!$this->driver->delete($key, $timeout)) {
            return new Error('Delete key fail');
        }
        return true;
    }

    public function flush()
    {
        return $this->driver->flush();
    }

    public function increment($key, $value = 1)
    {
        $result = $this->driver->increment($key, $value);
        if (!$result) {
            return new Error('Increment error');
        }
        return $result;
    }

    public function decrement($key, $value = 1)
    {
        $result = $this->driver->decrement($key, $value);
        if (!$result) {
            return new Error('Decrement error');
        }
        return $result;
    }

    public function close()
    {
        return $this->driver->close();
    }
}
?>